package com.xyongfeng.foodManagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyongfeng.foodManagement.constant.MyConstant;
import com.xyongfeng.foodManagement.domain.AdminInfo;
import com.xyongfeng.foodManagement.domain.Permission;
import com.xyongfeng.foodManagement.domain.vo.AdminInfoSimpleVO;
import com.xyongfeng.foodManagement.domain.vo.AdminInfoVO;
import com.xyongfeng.foodManagement.mapper.AdminInfoMapper;
import com.xyongfeng.foodManagement.service.AdminInfoService;
import com.xyongfeng.foodManagement.service.PermissionService;
import com.xyongfeng.foodManagement.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xyongfeng
 * @description 针对表【admin_info】的数据库操作Service实现
 * @createDate 2023-12-17 17:02:37
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfo>
        implements AdminInfoService {

    @Autowired
    private AdminInfoMapper adminInfoMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;


    @Override
    public AdminInfoVO login(String username, String password, HttpServletRequest request) {
        AdminInfo adminInfo = getOne(new QueryWrapper<AdminInfo>()
                .eq("admin_username", username)
                .eq("admin_password", password));
        if (adminInfo == null) return null;

        request.getSession().setAttribute(MyConstant.LOGIN_USER, adminInfo.convertToVO());

        return adminInfo.convertToVO();
    }

    @Override
    public AdminInfoVO logout(HttpServletRequest request) {

        AdminInfoVO adminInfo = getLoginAdmin(request);

        request.getSession().setAttribute(MyConstant.LOGIN_USER, null);

        return adminInfo;
    }

    @Override
    public AdminInfoVO getLoginAdmin(HttpServletRequest request) {

        AdminInfoVO adminInfo = (AdminInfoVO) request.getSession().getAttribute(MyConstant.LOGIN_USER);
        if (adminInfo == null) return null;

        return getById(adminInfo.getAdminId()).convertToVO();
    }

    @Override
    public List<AdminInfoVO> selectAll() {

        List<AdminInfoVO> adminInfos = adminInfoMapper
                .selectList(new QueryWrapper<>()).stream()
                .map(AdminInfo::convertToVO).filter(x -> !x.getAdminId().equals(1))
                .collect(Collectors.toList());
        return adminInfos;
    }

    @Override
    public AdminInfo findByUsername(String username) {
        AdminInfo adminInfo = lambdaQuery().eq(AdminInfo::getAdminUsername, username).one();

        // 查询角色权限
        List<Permission> permissions = permissionService.getByAdminId(adminInfo.getAdminId());
        List<String> permissionsStr = permissions.stream().map(Permission::getPermission).collect(Collectors.toList());

        adminInfo.setPermissions(permissionsStr);
        return adminInfo;
    }

    @Override
    public List<AdminInfoSimpleVO> selectSimpleAll() {
        List<AdminInfoSimpleVO> list = list().stream().map(x -> {
            AdminInfoSimpleVO adminInfoSimpleVO = new AdminInfoSimpleVO();

            adminInfoSimpleVO.setAdminName(x.getAdminName());
            adminInfoSimpleVO.setAdminId(x.getAdminId());
            return adminInfoSimpleVO;
        }).collect(Collectors.toList());
        return list;

    }
}




